package com.shop.dao.impl;

import com.shop.dao.IUserDao;
import com.shop.entity.User;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserDaoImpl extends BaseDao implements IUserDao {
    public Map<String,String> getMap(){
        Map<String,String> map = new HashMap<>();
        map.put("u_id","uid");
        map.put("u_name","username");
        map.put("u_password","upassword");
        map.put("u_sex","usex");
        map.put("u_status","ustatus");
        map.put("u_code","code");
        map.put("u_email","email");
        map.put("u_role","urole");
        return map;
    }

    @Override
    public List<User> selectAllUser() {
        String sql = "select * from user";
        return queryForList(User.class, sql, getMap());
    }

    @Override
    public boolean insertUser(User user) {
        System.out.println(user);
        String sql = "insert into user values(null,?,?,?,?,?,?,?)";
        Object[] objects = new Object[]{user.getUsername(), user.getUpassword(), user.getEmail(), user.getUsex(), user.getUstatus(), user.getCode(), user.getUrole()};
        return update(sql,objects) > 0;
    }
    @Override
    public boolean updateUser(String code) {
        String sql = "update user set u_status = 1 where u_code = ?";
        return update(sql,code) > 0;
    }

    @Override
    public User selectUserByName(String username) {
        String sql = "select * from user where u_name =?";
        return queryForOne(User.class,sql,getMap(),username);
    }

}
